const Clutter = imports.gi.Clutter;
const Cogl    = imports.gi.Cogl;
const GdkPixbuf = imports.gi.GdkPixbuf;
const GLib    = imports.gi.GLib;
const Lang    = imports.lang;
const Mx      = imports.gi.Mx;
const Octopus = imports.gi.Octopus;
const OctopusMx = imports.gi.OctopusMx;
const Signals = imports.signals;

function AlbumArt(container, albumArtSize) {
	this._init(container, albumArtSize);
}

AlbumArt.prototype = {
	_init: function (container, albumArtSize) {
		this._albumArtSize = albumArtSize;
		this.albumArtLoader = new Octopus.AlbumArtLoader ();
		this.albumArtLoader.connect ('bindata', Lang.bind(this, function (/*AlbumArtLoader*/obj) {
			//process xmms bindata
			try {
				var loader = new GdkPixbuf.PixbufLoader();
				loader.write(this.albumArtLoader.get_bindata ());
				loader.close();
				//we may want to scale
				//var unscaled = loader.get_pixbuf();
				//var pixbuf = unscaled.scale_simple (150, 150, /*GdkPixbuf.InterpType.bilinear*/ 2);
				var pixbuf = loader.get_pixbuf();
				this.albumArt.set_from_data (pixbuf.get_pixels (),
				                             pixbuf.has_alpha ? Cogl.PixelFormat.RGBA_8888
				                                              : Cogl.PixelFormat.RGB_888,
				                             pixbuf.width,
				                             pixbuf.height,
				                             pixbuf.rowstride);
				this.albumArt.visible = true;
				//insert into Mx textureCache
				OctopusMx.utils_texture_cache_insert (this.picture_front, pixbuf);
			} catch (e) {
				print ("error: " + e.message);
			} finally {
				this.emit ('album-art-loaded');
			}
		}));

		this.albumArtStack = new Mx.Stack ({ name: 'albumArt-background' });
		container.add_child (this.albumArtStack);

		//defalt album art, we always show this, in case something goes wrong
		this.defaultAlbumArt = new Mx.BoxLayout ({ style_class: 'albumArt-default',
		                                           orientation: Mx.Orientation.VERTICAL,
		                                           width: this._albumArtSize,
		                                           height: this._albumArtSize,
		                                           x_align: Clutter.ActorAlign.START,
		                                           y_align: Clutter.ActorAlign.START });
		this.defaultAlbumArtArtist = new Mx.Label ({ style_class: 'albumArt-default-artist' });
		this.defaultAlbumArt.add_child (this.defaultAlbumArtArtist);
		this.defaultAlbumArtAlbum = new Mx.Label ({ style_class: 'albumArt-default-album',
		                                            line_wrap: true });
		this.defaultAlbumArt.add_child (this.defaultAlbumArtAlbum);
		this.albumArtStack.add_child (this.defaultAlbumArt);

		this.albumArt = new Mx.Image ({ style_class: 'albumArt-image',
		                                width: this._albumArtSize,
		                                height: this._albumArtSize,
		                                scale_mode: Mx.ImageScaleMode.FIT,
		                                x_align: Clutter.ActorAlign.START,
		                                y_align: Clutter.ActorAlign.START,
		                                visible: false });
		this.albumArtStack.add_child (this.albumArt);

		this.albumArtOverlay = new Mx.Frame ({ name: 'albumArt-overlay' });
		this.albumArtStack.add_child (this.albumArtOverlay);
	},

	loadAlbumArt: function (picture_front) {
		this.picture_front = picture_front;
		this.albumArt.visible = false;
		if (   (picture_front != null)
		    && (picture_front != "")) {
			let filename = GLib.build_filenamev ([global.bindata_path, picture_front]);
			//step 1: check if picture_front is already in textureCache
			if (Mx.TextureCache.get_default ().contains (picture_front)) {
				this.setFromTextureCache (picture_front);
			} else {
				//step 2: load from filesystem
				try {
					var pixbuf = GdkPixbuf.Pixbuf.new_from_file (filename);
					//insert into Mx textureCache
					OctopusMx.utils_texture_cache_insert (this.picture_front, pixbuf);
					this.setFromTextureCache (picture_front);
				} catch (e) {
					//step 3: load from xmms
					this.albumArtLoader.xmms_bindata_retrieve (picture_front);
				}
			}
		} else {
			this.emit ('album-art-loaded');
		}
	},

	setFromTextureCache: function (picture_front) {
		try {
			this.albumArt.set_from_file_at_size (picture_front, -1, -1,
			                                     null);
			this.albumArt.width = this._albumArtSize;
			this.albumArt.height = this._albumArtSize;
			this.albumArt.visible = true;
		} catch (e) {
			print ("error loading albumArt from Mx.TextureCache; name: " + picture_front + " message: " + e.message);
		} finally {
			this.emit ('album-art-loaded');
		}
	}
};

Signals.addSignalMethods(AlbumArt.prototype);
